// 商品模型
const { DataTypes } = require('sequelize');
const { sequelize } = require('../utils/sequelize');
const config = require('../config/database');

const Goods = sequelize.define(
  'BidGoods', // 模型名称
  {
    id: {
      type: DataTypes.STRING(50),
      primaryKey: true,
      allowNull: false,
      comment: '商品ID'
    },
    seller_id: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: '卖家ID'
    },
    type: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: '交易类型：自由交易、平台托管'
    },
    trade_type: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: '交易方式：上门自提、到店自提、线上交易'
    },
    trade_details: {
      type: DataTypes.TEXT,
      comment: '交易方式详情'
    },
    title: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '商品标题'
    },
    subtitle: {
      type: DataTypes.STRING(200),
      comment: '商品副标题'
    },
    description: {
      type: DataTypes.TEXT,
      comment: '商品描述'
    },
    category_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '分类ID'
    },
    current_price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      comment: '当前价格'
    },
    original_price: {
      type: DataTypes.DECIMAL(10, 2),
      comment: '原价'
    },
    price_drop: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '降价金额'
    },
    cheaper_than_similar: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '比同类便宜'
    },
    image: {
      type: DataTypes.STRING(255),
      comment: '主图URL'
    },
    status: {
      type: DataTypes.STRING(20),
      defaultValue: 'onSale',
      comment: '商品状态：onSale-在售，sold-已售，off-下架'
    }
  },
  {
    tableName: config.mysql.prefix + 'goods',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['seller_id'], name: 'idx_seller_id' },
      { fields: ['category_id'], name: 'idx_category_id' }
    ]
  }
);

module.exports = Goods;